package util;


public class BinaryTreeUtil {

    /**
     * 生成二叉树的方法
     */
    public static void main(String[] args) {
        Integer[] data = new Integer[]{5,4,8,11,null,13,14,7,2,null,null,null,1};

        // 给定一个数组生成二叉树
        TreeNode root = build(1,data);
        System.out.println(root);
    }

    /**
     * 生成二叉树
     */
    public static TreeNode build(int index, Integer[] data){
        if(index -1 > data.length-1){
            return null ;
        }

        Integer currVal = data[index - 1];
        if(currVal == null){
            return null ;
        }
        TreeNode curr = new TreeNode(currVal);

        int leftIndex = index * 2 ;
        int rightIndex = leftIndex + 1;

        // 构建当前节点的子树
        curr.left = build(leftIndex, data);
        curr.right = build(rightIndex, data);
        return curr ;
    }



}
